import openpyxl
import cadquery as cq

# 打开 Excel 文件
wb = openpyxl.load_workbook("test.xlsx")

# 获取特定列的数据
sheet = wb.active
column_data = [cell.value for cell in sheet["A"]]
print(column_data)
# 提取点坐标
points = []
# for row in column_data.split(",")[2:-4]:
for row in column_data:
    row = row.split(",")[1:4]
    point_number, x_coordinate, y_coordinate = row
    points.append((point_number, float(x_coordinate), float(y_coordinate)))
    print(row) 
# 设置投影坐标系
projection = cq.CoordinateSystem.from_proj4("+proj=utm +zone=51 +datum=WGS84")

# 创建 CAD 对象
cad = cq.Workplane(projection)

# 按照点号顺序投影点
for point_number, x_coordinate, y_coordinate, elevation in points:
    point = cad.new_point((x_coordinate, y_coordinate, elevation))

# 连线
cad.new_line(points[0][1], points[-1][1])
for i in range(len(points) - 1):
    cad.new_line(points[i][1], points[i + 1][1])

# 导出 CAD 文件
cad.export("points.dxf")


import ezdxf

data = "1,10,1,20,30,2,21,29,3,22,22,50,40,,1,profile picture"

# 解析数据
data_list = data.split(",")
total_points = int(data_list[1])
elevation_start = int(data_list[-4])
elevation_end = int(data_list[-3])

points = []
for i in range(2, len(data_list)-5, 3):
    point_num = int(data_list[i])
    x = int(data_list[i+1])
    y = int(data_list[i+2])
    points.append((point_num, x, y))

# 投影点坐标
projected_points = [(point_num, x, y, elevation_start) for point_num, x, y in points]

# 连接点成线
lines = [(projected_points[i][1:], projected_points[i+1][1:]) for i in range(len(projected_points)-1)]

# 创建CAD文件
doc = ezdxf.new("R2010")
msp = doc.modelspace()

for line in lines:
    msp.add_line(line[0][0], line[0][1], line[0][2], line[1][0], line[1][1], line[1][2])

cad_filename = "output.dxf"
doc.saveas(cad_filename)

print(f"CAD文件已生成：{cad_filename}")
